#include "planet.h"

using namespace std;

int Planet::total = 0;
ifstream &operator>>(ifstream &in, Planet &planet)
{
  char buf[BUF_L];
  char end;
  char *bufName = nullptr;
  int bufDiam;
  bool bufLife;
  int bufSat;
  if (in.getline(buf, BUF_L, '\t') && in >> bufDiam && in >> bufLife && in >> bufSat && in.get(end))
  {
    int l_name = strlen(buf) + 1;
    bufName = new char[l_name];
    strncpy(bufName, buf, l_name);
    Planet bufPlanet(bufName, bufDiam, bufLife, bufSat);
    planet = bufPlanet;
  }
  delete[] bufName;
  return in;
}

ofstream &operator<<(ofstream &out, Planet &planet)
{
  out << planet.getName() << '\t' << planet.getDiam() << ' ' << planet.getLife() << ' ' << planet.getSat() << endl;
  return out;
}

void read_database(const char *&filename, Planet *&planets, int &planets_count)
{
  ifstream in(filename, ios::in);
  if (!in.is_open())
  {
    cout << "Ошибка открытия файла" << endl;
    return;
  }
  Planet *state_planets;
  Planet new_planet;
  while (in >> new_planet)
  {
    ++planets_count;
    state_planets = new Planet[planets_count];
    for (int i = 0; i < planets_count - 1; ++i)
    {
      state_planets[i] = planets[i];
    }
    delete[] planets;
    state_planets[planets_count - 1] = new_planet;
    planets = state_planets;
  }
  in.close();
}

void write_database(const char *&filename, Planet *&planets, int &planets_count)
{
  ofstream out(filename, ios::out);
  if (!out.is_open())
  {
    cout << "Не удаётся создать файл" << endl;
    return;
  }
  for (int i = 0; i < planets_count; ++i)
  {
    out << planets[i];
  }
  cout << "Солнечная система сохранена в файл" << endl;
  out.close();
}

void sort_database(Planet *&planets, const int &planets_number)
{
  for (int i = 0; i + 1 < planets_number; ++i)
  {
    for (int j = 0; j + 1 < planets_number - i; ++j)
    {
      if (planets[j] > planets[i + 1])
      {
        Planet bufPlanet = planets[i];
        planets[j] = planets[j + 1];
        planets[j + 1] = bufPlanet;
      }
    }
  }
}
void add(Planet *&planets, int &planets_count)
{
  char buf[BUF_L];
  int bufDiam;
  bool bufLife;
  int bufSat;
  cout << "Введите имя\n";
  cin >> buf;
  int l_name = strlen(buf) + 1;
  char *bufName = new char[l_name];
  strncpy(bufName, buf, l_name);
  cout << "Введите диаметр\n";
  cin >> bufDiam;
  cout << "Нажмите 1,если жизнь есть. 0, если жизни нет\n";
  cin >> bufLife;
  cout << "Укажите кол-во спутников\n";
  cin >> bufSat;
  Planet new_planet(bufName, bufDiam, bufLife, bufSat);
  ++planets_count;
  Planet *planets_state = new Planet[planets_count];
  for (int i = 0; i < planets_count - 1; ++i)
  {
    planets_state[i] = planets[i];
  }
  delete[] planets;
  planets_state[planets_count - 1] = new_planet;
  planets = planets_state;
  delete[] bufName;
}

void remove(Planet *&planets, int &planets_count, const int &id)
{
  planets_count--;
  cout << "Планета " << planets[id].getName() << " уничтожена." << endl;
  for (int i = id; i < planets_count; ++i)
  {
    planets[i] = planets[i + 1];
  }
  Planet *state_planets = new Planet[planets_count];
  for (int i = 0; i < planets_count; ++i)
  {
    state_planets[i] = planets[i];
  }
  delete[] planets;
  planets = state_planets; //
}

void print_database(Planet *&planets, const int &planets_count)
{
  if (planets_count == 0)
  {
    cout << "Ничего нет" << endl;
    return;
  }
  for (int i = 0; i < planets_count; ++i)
  {
    planets[i].print();
  }
  cout << endl;
}

int find(Planet *&planets, const int &planets_count)
{
  cout << "Введите название планеты" << endl;
  string input;
  cin >> input;
  int ind = -1;
  for (int i = 0; i < planets_count; i++)
  {
    if (planets[i].getName() == input)
    {
      ind = i;
      planets[i].print();
      cout << endl;
      return i;
    }
  }
  cout << "Такой планеты нет" << endl;
  return ind;
}

int main()
{
    {
  int integer;
  cout << endl;
  cout << "Введите число от 1 до 7: ";

  if (cin >> integer)
  {
  }
  else
  {
    cin.clear();
    cin.ignore(1000, '\n');
  }

  return integer;
}
}